package Model.Beans.Results;

import Model.Beans.*;
import Model.XML.*;
import java.util.*;

public class CourseList {
    private ArrayList courses;
    private XMLWriter xml;
    
    public CourseList() 
    {
        courses = new ArrayList();
        xml = new XMLWriter();
    }
    
    public ArrayList getCourses()
    {
        return courses;
    }
    
    public void addCourse(Course course)
    {
        courses.add(course);
    }
    
    public void delCourse(Course course)
    {
        for(int i=0; i<courses.size(); i++)
        {
            Course tmp = (Course)courses.get(i);
            
            if(tmp.getId() == course.getId())
            {
                courses.remove(i);
            }
        }
    }
    
    public String toXML()
    {
        xml.Push("courses-list");
        String[] attributeName = new String[5];
        String[] attribute = new String[5];
        attributeName[0] = "professor-name";
        attributeName[1] = "time";
        attributeName[2] = "days";
        attributeName[3] = "id";
        attributeName[4] = "classroom";
        
        for(int i=0; i<courses.size(); i++)
        {
            Course tmp = (Course)courses.get(i);
                if(tmp.getProfessor() != null)
                    attribute[0] = tmp.getProfessor().toString();
                else
                    attribute[0] = "";
            attribute[1] = tmp.getTime();
            attribute[2] = tmp.getDays();
            attribute[3] = Integer.toString(tmp.getId());
            if(tmp.getClassRoom() != null)
                attribute[4] = tmp.getClassRoom().toString();
            else
                attribute[4] = "";
            xml.Element("course", tmp.toString(), attributeName, attribute);
        }
        xml.Pop();
        return xml.GetXML();
    }
}
